package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workcorrect.upd1;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskClass;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues.Article;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskClassRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.workcorrect.upd1.HomeworkCorrect1Service;

/**
 * 获取班级用户作业详情
 */
@CrossOrigin
@RestController
public class Workcorrect1FindClassStuTaskController extends YxyApiBaseController {

    @Autowired
    HomeworkCorrect1Service homeworkCorrect1Service;

    @Autowired
    HomeworkTaskClassRepository homeworkTaskClassRepository;

    @Autowired
    HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Autowired
    HomeworkTaskReportQuesRepository homeworkTaskReportQuesRepository;

    @Autowired
    HomeworkTaskQuesRepository homeworkTaskQuesRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/workcorrect1/stutask/find", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestParam String taskId, //
            @RequestParam String classId, //
            ReqModel reqModel, //
            RespModel respModel) throws ParseException {
        // ##
        // 获取用户基本信息
        try {
            reqModel.userId = getProfile(request, response).getId();
            reqModel.schoolId = getProfile(request, response).getSchoolId();
        } catch (Exception e) {
            Assert.isTrue(false, "用户信息异常，请重新登录。");
        }
        reqModel.taskId = taskId;
        reqModel.classId = classId;
        // ##
        // 获取任务基本信息
        HomeworkTaskClass homeworkTaskClass = homeworkTaskClassRepository.findTop1ByTaskIdAndClassId(taskId, classId);
        // ##
        // 获取作业班级用户
        List<HomeworkTaskReport> homeworkTaskReports = homeworkTaskReportRepository
                .findByTaskIdAndClassIdOrderByFinishedDateAsc(taskId, classId);
        // 获取有任务的班级学生
        List<String> students = new ArrayList<String>();
        homeworkTaskReports.forEach(homeworkTaskReport -> students.add(homeworkTaskReport.getStudentId()));
        // 本次任务是否有客观题(单选题，多选题)
        List<String> quesTypes = new ArrayList<String>();
        quesTypes.add("0");
        quesTypes.add("1");
        Integer count = homeworkTaskQuesRepository.countByTaskIdAndQuesTypeIn(taskId, quesTypes);
        // 获取学生主观题作业
        List<String> quesTypes1 = new ArrayList<String>();
        quesTypes1.add("3");
        quesTypes1.add("4");
        List<HomeworkTaskReportQues> HomeworkTaskReportQueslist1 = homeworkTaskReportQuesRepository
                .findByTaskIdAndQuestionTypeInAndStudentIdIn(taskId, quesTypes1, students);
        // 获取通知类作业
        List<HomeworkTaskReportQues> HomeworkTaskReportQueslist2 = homeworkTaskReportQuesRepository
                .findByTaskIdAndQuestionTypeAndStudentIdIn(taskId, "2", students);
        // 本次任务主观题作业试题
        List<HomeworkTaskQues> homeworkTaskQuesList = homeworkTaskQuesRepository
                .findByTaskIdAndQuesTypeInOrderByOrderAsc(taskId, quesTypes1);
        // ##
        // 对数据进行处理,转化为 RespModel 对象
        return homeworkCorrect1Service.convertRespModel(homeworkTaskClass, homeworkTaskReports,
                HomeworkTaskReportQueslist1, HomeworkTaskReportQueslist2, count, homeworkTaskQuesList);
    }

    /** 请求参数 **/
    public static class ReqModel extends YxyApiReqModel {
        // 任务Id
        public String taskId;
        // 班级Id
        public String classId;
        // 用户Id
        public String userId;
        // 学校Id
        public String schoolId;
    }

    /** 响应 **/
    public static class RespModel extends YxyApiRespModel {
        // 任务Id
        public String taskId;
        // 任务名称
        public String taskName;
        // 年级
        public String grade;
        // 班级名称
        public String className;
        // 是否有客观题
        public boolean is0bjective;
        // 班级学生任务
        public List<ClassStuTaskReport> classStuTaskReports;
    }

    public static class ClassStuTaskReport {
        // 任务Id
        public String taskId;
        // 班级Id
        public String classId;
        // 班级名称
        public String className;
        // userId
        public String studentId;
        // userNick
        public String studentNick;
        // user Img
        public String studentImg;
        // 学生是否提交作业
        public Integer statusOfFinished;
        // 客观题答对几道
        public Integer exactnessNumber;
        // 客观题答错几道
        public Integer wrongNumber;
        // 是否有通知类试题
        public boolean isNotify = false;
        // 通知类-试题Id
        public String notifyQuesId;
        // 通知类-试题序号
        public Integer notifyQuesOrder;
        // 通知类-评分
        public Integer notifyMark;
        // 通知类-文本答案
        public String notifyAnswerText;
        // 通知类-图片答案
        public List<String> notifyAnswerImgs;
        // 通知类-作业学生任务试题Id
        public String notifyTaskReportQuesId;
        // 通知类-试题内容
        public String notifyQuesContent;
        // 通知类-试题图片
        public List<String> notifyQuesImage;
        // 通知类-上传的文档
        public List<Article> notifyQuesArticle;
        // 答题时间
        public String notifyFinishTime;
        // 主观题
        public List<SubjectiveQues> subjectiveQuesList;
    }

    public static class SubjectiveQues {
        // 试题Id
        public String quesId;
        // 试题序号
        public Integer quesOrder;
        // 评分
        public Integer mark;
        // 文本答案
        public String answerText;
        // 图片答案
        public List<String> answerImgs;
        // 作业学生任务试题Id
        public String taskReportQuesId;
        // 总-知识点列表
        public List<Know> knows;
        // 勾选错误的知识点
        public List<Integer> wrongKnowIdLst;
        // 正确知识点
        public List<Integer> exactnessKnowIdLst;
        // 试题类型
        public String quesType;
        // 答题时间
        public String finishTime;
    }

    // 总-知识点列表
    public static class Know {
        public Integer id;
        public String name;
    }
}
